/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.time.format.DateTimeFormatter;

/**
 * <p>Project: jdbc202202 - MetaDataDemo
 * <p>Powered by webrx On 2022-08-22 14:44:52
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class MetaDataDemo {

    @Test
    void mdd1(){
        try{
            var c = DriverManager.getConnection("jdbc:mysql:/db?user=root");
            //System.out.println(c.getMetaData().getDatabaseProductVersion());
            DatabaseMetaData dmd = c.getMetaData();
            System.out.println(dmd.getDatabaseProductVersion());
            System.out.println(dmd.getDriverName());
            System.out.println(dmd.getDriverVersion());

            PreparedStatement pst = c.prepareStatement("select @@version,@@port,@@basedir,@@datadir,database()");
            ResultSet rs = pst.executeQuery();
            rs.next();
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
            System.out.println(rs.getString(4));
            System.out.println(rs.getString(5));
            System.out.println("------------------------------------");
            rs = pst.executeQuery("show tables");
            while(rs.next()){
                System.out.println(rs.getString(1));
            }
            System.out.println("------------------------------------");

            rs = pst.executeQuery("show databases");
            while(rs.next()){
                System.out.println(rs.getString(1));
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }


    @Test
    void mdd2(){
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","");
            PreparedStatement pst = conn.prepareStatement("select * from t_book");
            ResultSet rs = pst.executeQuery();

            ResultSetMetaData metaData = rs.getMetaData();
            System.out.println(metaData.getColumnCount());

            while(rs.next()){
                for(int i=1;i<=metaData.getColumnCount();i++){
                    //System.out.println(metaData.getColumnName(i));
                    //System.out.println(metaData.getColumnLabel(i));
                    //System.out.println(metaData.getColumnTypeName(i));
                    String k = metaData.getColumnName(i);
                    //93 datetime  91 date -5 long 3 double 12 varchar
                    //System.out.println(metaData.getColumnType(i));
                    if(metaData.getColumnType(i) == 93) {
                        Timestamp tt = rs.getTimestamp(i);
                        System.out.println(tt.toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒")));
                    }else{
                        System.out.println(rs.getObject(k));
                    }
                }
                System.out.println("-----------------------------------------");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
